
#include "TouchPanel.h"

#include <System/Int32.h>
#include <System/Diagnostics/Debug.h>
#include <Riccsson/Xna/Framework/RGame.h>

using namespace Riccsson::System;
using namespace Riccsson::System::Diagnostics;
using namespace Riccsson::Xna::Framework;
using namespace Riccsson::Xna::Framework::Input::Touch;

#if Windows
bool _TouchPanel_mouse_pressed = false;
bool _TouchPanel_mouse_pressed_last = false;
float _TouchPanel_mouse_last_x = 0.0f;
float _TouchPanel_mouse_last_y = 0.0f;
#else
#	include <UndefCSharpKeywords.h>
#	include <jni.h>
#	include <android/input.h>
#	include <android/sensor.h>
#	include <platforms/Xna/android/AndroidSetup.h>
#endif

// Initializing.
PROP3_STATIC_INIT(TouchPanel, int, DisplayHeight);
PROP3_STATIC_INIT(TouchPanel, DisplayOrientation, displayOrientation);
PROP3_STATIC_INIT(TouchPanel, int, DisplayWidth);
PROP3_STATIC_INIT(TouchPanel, GestureType, EnabledGestures);
PROP3GET_STATIC_INIT(TouchPanel, bool, IsGestureAvailable);
PROP3_STATIC_INIT(TouchPanel, Riccsson::System::IntPtr, WindowHandle);

PROP3_GET_CPP(TouchPanel, int, DisplayHeight)
{
	throw;
}
PROP3_SET_CPP(TouchPanel, int, DisplayHeight)
{
	throw;
}

PROP3_GET_CPP(TouchPanel, DisplayOrientation, displayOrientation)
{
	throw;
}
PROP3_SET_CPP(TouchPanel, DisplayOrientation, displayOrientation)
{
	throw;
}

PROP3_GET_CPP(TouchPanel, int, DisplayWidth)
{
	throw;
}
PROP3_SET_CPP(TouchPanel, int, DisplayWidth)
{
	throw;
}

PROP3_GET_CPP(TouchPanel, GestureType, EnabledGestures)
{
	throw;
}
PROP3_SET_CPP(TouchPanel, GestureType, EnabledGestures)
{
	throw;
}

PROP3_GET_CPP(TouchPanel, bool, IsGestureAvailable)
{
	throw;
}

PROP3_GET_CPP(TouchPanel, IntPtr, WindowHandle)
{
	throw;
}
PROP3_SET_CPP(TouchPanel, IntPtr, WindowHandle)
{
	throw;
}

TouchPanelCapabilities TouchPanel::GetCapabilities()
{
	throw;
}

TouchCollection TouchPanel::GetState()
{
#if Windows

	// TODO: Change this code so it will support multi touch screen instead for the mouse.

	SHORT result = GetAsyncKeyState(VK_LBUTTON);
	int mouseIsActive = result != 0 ? 1 : 0;

	Array<TouchLocation> listOfLocations(mouseIsActive);
	
	POINT p;
	if (GetCursorPos(&p) && mouseIsActive)
	{
		_TouchPanel_mouse_pressed = true;

		RECT rect;
		GetWindowRect(g_hWnd, &rect);
		
		p.x -= rect.left;
		p.y -= rect.top;
		
		_TouchPanel_mouse_last_x = (float)p.x;
		_TouchPanel_mouse_last_y = (float)p.y;

		if(_TouchPanel_mouse_pressed && !_TouchPanel_mouse_pressed_last)
		{
			// PRESSED.
			TouchLocation location(0, TouchLocationState::Pressed, Vector2((float)p.x, (float)p.y));
			listOfLocations.Insert(0, location);
		}
		else if(_TouchPanel_mouse_pressed && _TouchPanel_mouse_pressed_last)
		{
			// MOVING.
			TouchLocation location(0, TouchLocationState::Moved, Vector2((float)p.x, (float)p.y));
			listOfLocations.Insert(0, location);
		}
	}

	if(!_TouchPanel_mouse_pressed && _TouchPanel_mouse_pressed_last)
	{
		listOfLocations = Array<TouchLocation>(1);

		// RELEASE.
		TouchLocation location(0, TouchLocationState::Released, Vector2(_TouchPanel_mouse_last_x, _TouchPanel_mouse_last_y));
		listOfLocations.Insert(0, location);
	}

	_TouchPanel_mouse_pressed_last = _TouchPanel_mouse_pressed;

#elif Android
	// Collection to return.
	Array<TouchLocation> listOfLocations(_android_touchStates.size());

	// Loop every pointer.
	for (int i = 0; i < _android_touchStates.size(); i++)
	{
		_android_TouchState& _state = _android_touchStates[i];
		
		TouchLocationState action;
		switch(_state.action)
		{
		case _ADNROID_ACTION_POINTER_DOWN:
		case _ADNROID_ACTION_DOWN:
			action = TouchLocationState::Pressed;
			break;
		case _ADNROID_ACTION_POINTER_UP:
		case _ADNROID_ACTION_UP:
			action = TouchLocationState::Released;
			break;
		case _ADNROID_ACTION_MOVE:
			action = TouchLocationState::Moved;
			break;
		default:
			action = TouchLocationState::Invalid;
			break;
		}

		TouchLocation location(_state.touchId / 100, action, Vector2(_state.x, _state.y));
		
		listOfLocations.Insert(i, location);
	}
	
	// Now. Clear all queued touch inputs from internal.
	_android_touchStates.clear();
#endif

	// Return the list of current touch locations.
	return TouchCollection(&listOfLocations);
}

GestureSample TouchPanel::ReadGesture()
{
	throw;
}
